import { IFirst, IMargin, IDetaile, IMgDetaile, IYearC } from '@/sevices/index';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { IFI, IMa, IDe, IMde } from '@/utils/interface';

export interface IFF {
  IFIS: IFI[];
  obj: any;
  IMas: IMa[];
  IDes: IDe[];
  yearList: [];
  IMdes: IMde[];
  IYea: [];
}
export interface IFC {
  namespace: 'user';
  state: IFF;
  effects: {
    IFirst: Effect;
    IMargin: Effect;
    IDetaile: Effect;
    IMgDetaile: Effect;
    IYearC: Effect;
  };
  reducers: {
    save: Reducer<IFI>;
  };
}

const Classify: IFC = {
  namespace: 'user',
  state: {
    IFIS: [],
    obj: {},
    IMas: [],
    IDes: [],
    yearList: [],
    IMdes: [],
    IYea: [],
  },
  effects: {
    //获取首页汽车数据
    *IFirst({ payload }, { call, put }) {
      let result = yield call(IFirst);
      const str = [
        'A',
        'B',
        'C',
        'D',
        'F',
        'G',
        'H',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'W',
        'X',
        'Y',
        'Z',
      ];
      const obj: any = {};
      str.forEach((item, index) => {
        (obj[item] = []),
          (result.data as IFI[]).forEach((i) => {
            if (i.Spelling[0].toLocaleUpperCase() === item) {
              obj[item].push(i);
            }
          });
      });
      yield put({
        type: 'save',
        payload: { IFIS: result.data, obj },
      });
    },
    //获取右边弹出盒子的数据
    *IMargin({ payload }, { call, put }) {
      let result = yield call(IMargin, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { IMas: result.data },
        });
      }
    },
    //详情数据
    *IDetaile({ payload }, { call, put }) {
      let result = yield call(IDetaile, payload);
      const list1: any[] = ['全部']; //定义一个空数组
      console.log(result.data);
      //遍历整个数据
      (result?.data?.list as any)?.map((item: any) => {
        console.log(item);
        //判断这个空数组里是否有这个数据
        if (list1.indexOf(item?.market_attribute?.year) === -1) {
          //没有的话就把他给push进去
          list1.push(item?.market_attribute?.year);
        }
      });
      yield put({
        type: 'save',
        payload: { IDes: result.data, yearList: list1 },
      });
    },

    //图片详情
    *IMgDetaile({ payload }, { call, put }) {
      let result = yield call(IMgDetaile, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { IMdes: result.data },
        });
      }
    },
    //颜色详情
    *IYearC({ payload }, { call, put }) {
      let result = yield call(IYearC, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { IYea: result.data },
        });
      }
    },
  },
  // 同步操作，理解为vuex中的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
};

export default Classify;
